package com.example.statistics.weight

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.example.statistics.PriceItemBean
import com.example.statistics.R
import com.example.statistics.ex.format
import com.example.statistics.ex.isNumber
import com.example.statistics.ex.parse
import com.example.statistics.ex.timePicker
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jhj.slimadapter.SlimAdapter
import kotlinx.android.synthetic.main.layout_price.view.*
import kotlinx.android.synthetic.main.list_item_price.view.*
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.toast
import org.w3c.dom.Text
import java.util.*

class PriceLayout : LinearLayout {

    private var isAdd = true

    val mView: View
    val adapter = SlimAdapter.creator()


    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mView = LayoutInflater.from(context).inflate(R.layout.layout_price, this, false)
        val typedArray = context?.obtainStyledAttributes(attrs, R.styleable.PriceLayout)
        val title = typedArray?.getString(R.styleable.PriceLayout_pl_title)
        isAdd = typedArray?.getBoolean(R.styleable.PriceLayout_pl_isAdd, true) ?: true
        mView.tv_title.text = title
        initView()
        typedArray?.recycle()
        addView(mView)
    }

    fun getData(): String {
        for (i in 0 until mView.recycler_view_price.childCount) {
            val layout = mView.recycler_view_price.getChildAt(i)
            val label = layout.find<EditText>(R.id.et_label).text?.toString()
            val content = layout.find<EditText>(R.id.et_content).text?.toString()
            val date = layout.find<TextView>(R.id.tv_date).text?.toString()
            val bean = adapter.getDataList<PriceItemBean>()[i]
            bean.label = label
            bean.content = content
            bean.date = date
        }
        val dataList = adapter.getDataList<PriceItemBean>()
        val list = dataList.filter { !it.label.isNullOrBlank() && !it.content.isNullOrBlank() }
        return Gson().toJson(list)
    }

    fun setData(str: String) {
        if (!str.isBlank()) {
            val dataList = Gson().fromJson<List<PriceItemBean>>(str, object : TypeToken<List<PriceItemBean>>() {}.type)
            var number: Double = 0.0
            dataList.forEach {
                if (it.content != null && it.content.isNumber()) {
                    number += it.content!!.toDouble()
                }
            }
            mView.tv_number.text = number.toString()
            adapter.addDataList(0, dataList)
        }

    }


    fun initView() {
        val priceList = mutableListOf<PriceItemBean>()
        priceList.add(PriceItemBean())
        adapter
                .register<PriceItemBean>(R.layout.list_item_price) { injector, bean, position ->
                    val tvDate = injector.getView<TextView>(R.id.tv_date)
                    injector.text(R.id.et_label, bean.label)
                            .text(R.id.et_content, bean.content)
                            .text(R.id.tv_date, bean.date)
                            .clicked(R.id.iv_date) {
                                val date = tvDate.text.toString().parse()
                                        ?: Calendar.getInstance().time
                                context.timePicker(date) {
                                    tvDate.text = it.format()
                                }
                            }
                }
                .attachTo(mView.recycler_view_price)
                .setDataList(priceList)

        mView.ic_add.onClick {
            for (i in 0 until mView.recycler_view_price.childCount) {
                val layout = mView.recycler_view_price.getChildAt(i)
                val label = layout.find<EditText>(R.id.et_label).text?.toString()
                val content = layout.find<EditText>(R.id.et_content).text?.toString()
                val date = layout.find<TextView>(R.id.tv_date).text?.toString()
                val bean = adapter.getDataList<PriceItemBean>()[i]
                bean.label = label
                bean.content = content
                bean.date = date
                if (content.isNullOrBlank() || label.isNullOrBlank()) {
                    context.toast("请补充信息完整")
                    return@onClick
                }
            }
            adapter.addData(PriceItemBean())
        }
    }


}